package 程序.基础;

import java.util.Scanner;
public class 狼人杀 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎来到狼人杀游戏");
        //定义游戏分组
        String[] roles = {"狼人","狼人","村民","村民","村民"};
        int numPlayers = roles.length;//玩家数量
        boolean[] isAlive = new boolean[numPlayers];//存活人数状态
        for (int i = 0; i < numPlayers; i++) {
            isAlive[i] = true;
        }
        boolean gameOver = false;//游戏结束变量
        while (!gameOver) {
            System.out.println("\n当前玩家：");
            for (int i = 0; i < numPlayers; i++) {
                if (isAlive[i]) {
                    System.out.println(i+"："+roles[i]);
                }//输出场上玩家
            }
            //狼人行动
            System.out.println("\n狼人请行动（输入杀死的玩家编辑）：");
            int wolfTarget = sc.nextInt();//输入编号
            if (wolfTarget<5&&wolfTarget>=0){
                //输入合法化
            }else {
                System.out.println("没有此玩家，请从新输入：");
                wolfTarget = sc.nextInt();
            }
            if (roles[wolfTarget].equals("狼人")) {
                System.out.println("此为队友确定要杀吗？(y/其他)");
                String Confirm = sc.next();
                if (Confirm.charAt(0) == 'y') {
                    isAlive[wolfTarget]=false;
                }
            }else {
                System.out.println("玩家"+wolfTarget+"以被杀死。");
                isAlive[wolfTarget]=false;
            }
            //判断游戏是否结束
            int numWolves = 0;//狼的数量
            int numVillager = 0;
            for (int i = 0; i < numPlayers; i++) {
                if (isAlive[i]) {
                    if (roles[i].equals("狼人")){
                        numWolves++;
                    }else {
                        numVillager++;
                    }
                }
            }
            if (numWolves >= numVillager){
                System.out.println("\n狼人胜利！");
                gameOver = true;
                break;
            }else if (numWolves==0){
                System.out.println("\n村民胜利！");
                gameOver=true;
                break;
            }else {

            }
            //平民行动
            if (!gameOver){
                System.out.println("\n天亮了，请发言并进行投票（输入要投票的玩家编号）：");
                int voteTarget = sc.nextInt();
                if (isAlive[voteTarget]){
                    System.out.println("玩家"+voteTarget+"已被投票出局。");
                    isAlive[voteTarget]=false;
                }else {
                    System.out.println("该玩家已经死亡或者不存在！");
                }
                //判断游戏是否结束
                numWolves=0;
                numVillager=0;
                for (int i = 0; i < numPlayers; i++) {
                    if (roles[i].equals("狼人")) {
                        numWolves++;
                    }else {
                        numVillager++;
                    }
                }
                if (numWolves>numVillager){
                    System.out.println("\n狼人胜利");
                    gameOver=true;
                }else if (numWolves==0){
                    System.out.println("\n村名胜利！");
                    gameOver=true;
                }
            }

        }
        System.out.println("\n游戏结束，再见！");
    }

}
